<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/31
 * Time: 10:04
 */
class DefaultGetMsgStrategy implements GetMsgStrategy
{

    protected $type = null;
    protected $limit = 100;

    public function __construct($type, $limit = 100)
    {
        $this->type = $type;
        $this->limit = $limit;
    }


    public function getItemList($type = null, $limit = 100)
    {
        $type = empty($type) ? $this->type : $type;
        $items = array();
        switch ($type) {
            case MsggItem::TYPE_CALL:
                $items = $this->getCallList();
                break;
            case MsggItem::TYPE_MAIL:
                $items = $this->getMailList();
                break;
            case MsggItem::TYPE_SMS:
                $items = $this->getSmsList();
                break;
            case MsggItem::TYPE_WECHAT:
                $items = $this->getWechatList();
                break;
        }
        return $items;
    }

    protected function getSmsList()
    {
        return $this->defaultGetList();
    }

    protected function getCallList()
    {
        return $this->defaultGetList();
    }

    protected function getWechatList()
    {
        return $this->defaultGetList();
    }

    protected function getMailList()
    {
        return $this->defaultGetList();
    }

    //default is get waiting retrying item limit 100 for user
    protected function defaultGetList()
    {
        $items = array();
        if (!empty($this->type) && !empty($this->limit)) {
            $items = MsggItem::model()->findAllByAttributes(array(
                'status' => array(
                    MsggItem::STATUS_WAITING,
                    MsggItem::STATUS_RETRYING
                ),
                'type' => $this->type,
            ), array('limit' => $this->limit, 'order' => 'level DESC , time'));
        }
        return $items;
    }


}